package cn.com.kblog.controller.admin;


import java.util.ArrayList;
import java.util.List;

import cn.com.kblog.common.Constant;
import cn.com.kblog.common.Result;
import cn.com.kblog.model.Blog;
import cn.com.kblog.model.BlogType;
import cn.com.kblog.model.Tag;
import cn.com.kblog.util.UploadUtils4BOS;

import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.render.JsonRender;
import com.jfinal.upload.UploadFile;

@ControllerBind(controllerKey = "/admin/blog",viewPath = "/WEB-INF/pages/admin/blog")
public class BlogController extends Controller{
	
	
	
	/**
	 * 分页查询Blog
	 */
	public void list(){
		Integer pageNumber = getParaToInt(0,Constant.PAGE_NUM_DEFAULT);
		Integer pageSize = getParaToInt("pageSize", Constant.PAGE_SIZE_DEFAULT);
		String orderField = getPara("orderField", Constant.PAGE_ORDER_FIELD_DEFAULT);
		String orderDirection = getPara("orderDirection", Constant.PAGE_ORDER_DIRECTION_DEFAULT);
		Page<Blog> beans = Blog.dao.pageQuery(pageNumber, pageSize, orderField, orderDirection, null);
		setAttr(Constant.BEANS_KEY, beans);
		render("blog_list.html");
	}
	
	/**
	 * 编辑Blog
	 */
	public void edit(){
		String id = getPara(0);
		//获得所有标签
		setAttr(Constant.BEANS_KEY, Tag.dao.listAll());
		//获得所有类型
		setAttr("blogtypes", BlogType.dao.listAll());
		if (StringKit.isBlank(id)) {
		    render("blog_edit.html");
		    return;
		}
		Blog bean = Blog.dao.findById(id);
		if (bean == null) {
			render("blog_edit.html");
		    return;
		}
		setAttr(Constant.BEAN_KEY, bean);
		render("blog_edit.html");
	}
	/**
	 * 保存Blog
	 */
	public void save(){
		Blog bean = getModel(Blog.class,Constant.BEAN_KEY);
		if(bean==null){
			keepPara();
			Result result =new Result(false, "参数错误!");
			setAttr(Constant.RESULT_KEY, result);
			render("blog_edit.html");
			return;
		}
		//保存Tag与Blog关系
		List<String> allTagIds =  new ArrayList<String>();
		
		//原有标签
		String[] tags = getParaValues("tag_id");
		if(tags != null){
			for (String string : tags) {
				allTagIds.add(string);
			}
		}
		//新标签
		String newTagNames = getPara("new_tag");
		if(StringKit.notBlank(newTagNames)){
			List<Integer> newTagList = Tag.dao.saveByList(newTagNames);
			for (Integer tag : newTagList) {
				allTagIds.add(""+tag);
			}
		}
		//设置作者
		Result result = bean.saveOrUpdate(allTagIds);
		setAttr(Constant.RESULT_KEY, result);
		list();
	}
	
	
	/**
	 * 删除Blog
	 */
	public void delete(){
		String id = getPara(0);
		if (StringKit.isBlank(id)) {
			renderJson(new Result(false, "信息错误！"));
		    render("blog_list.html");
		    return;
		}
		Result result = Blog.dao.deleteById(id);
		renderJson(result);
	}
	
	
	/**
	 * editor上传图片
	 */
	public void editor_upload(){
		try {
			//String tmpfsPath = BaeEnv.getTmpfsPath();
			UploadFile file = getFile("imgFile");
			String oldname = file.getFileName();
			if(_checkImageType(oldname)){
				setAttr("error", 1);
	            setAttr("message", "上传出错，请检查图片格式！");
	            render(new JsonRender().forIE());
	            return;
			}
			String url = UploadUtils4BOS.uploadFile(file.getFile());
			setAttr("error", 0);
	        setAttr("url", url);
	        render(new JsonRender().forIE());
		} catch (Exception e) {
			e.printStackTrace();
			setAttr("error", 1);
            setAttr("message", "上传出错，请稍后再试！");
            render(new JsonRender().forIE());
            return;
		}
	
	}
	
	/**
	 * 检查图片类型是否合法
	 * @param file
	 * @return
	 */
	private  boolean _checkImageType(String filename) {
		String fileType = filename.substring(filename.lastIndexOf("."), filename.length());
		if(Constant.IMAGE_TYPE.indexOf(fileType.toLowerCase()) == -1)
			 return true;
		return false;
	}
	
	
}
